package com.sky.wsp.spring.jackson.serialize;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 此注释会自动将 Serialize和Deserializer注册到jackson之中。
 *
 * @author 24044
 */
public class CustomerJackSon {
    /**
     * 手机号脱敏序列化
     */
    public static class MobileSerialize extends JsonSerializer<String> {
        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (null == value || value.length() != 11) {
                jsonGenerator.writeString(value);
                return;
            }
            Boolean mobile = (Boolean) serializerProvider.getAttribute("mobile");
            if (null == mobile || !mobile) {
                jsonGenerator.writeString(value);
                return;
            }
            jsonGenerator.writeString(value.substring(0, 3) + "****" + value.substring(7));
        }
    }

    /**
     * 手机号脱敏数据检测并丢弃
     */
    public static class MobileDeSerialize extends JsonDeserializer<String> {

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            if (null == value || value.length() != 11) {
                return value;
            }
            Boolean mobile = (Boolean) ctxt.getAttribute("mobile");
            if (null == mobile || !mobile) {
                return value;
            }
            if (value.contains("*")) {
                return null;
            }
            return value;
        }
    }

    /**
     * 邮箱脱敏序列化
     */
    public static class EmailSerialize extends JsonSerializer<String> {
        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (null == value || value.length() < 5) {
                jsonGenerator.writeString(value);
                return;
            }
            String[] split = value.split("@");
            if (split.length != 2 || split[0].length() < 2 || split[1].length() < 2) {
                jsonGenerator.writeString(value);
                return;
            }
            Boolean email = (Boolean) serializerProvider.getAttribute("email");
            if (null == email || !email) {
                jsonGenerator.writeString(value);
                return;
            }
            jsonGenerator.writeString(split[0].substring(0, 2) + "****@" + split[1]);
        }
    }

    /**
     * 邮箱脱敏数据检测并丢弃
     */
    public static class EmailDeSerialize extends JsonDeserializer<String> {

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            if (null == value || value.length() < 5) {
                return value;
            }
            Boolean email = (Boolean) ctxt.getAttribute("email");
            if (null == email || !email) {
                return value;
            }
            if (value.contains("*")) {
                return null;
            }
            return value;
        }
    }

    /**
     * 身份证脱敏序列化
     */
    public static class IdentitySerialize extends JsonSerializer<String> {
        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (null == value || value.length() != 18) {
                jsonGenerator.writeString(value);
                return;
            }
            Boolean identity = (Boolean) serializerProvider.getAttribute("identity");
            if (null == identity || !identity) {
                jsonGenerator.writeString(value);
                return;
            }
            jsonGenerator.writeString(value.substring(0, 3) + "***********" + value.substring(14));
        }
    }

    /**
     * 身份证脱敏数据检测并丢弃
     */
    public static class IdentityDeSerialize extends JsonDeserializer<String> {

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            if (null == value || value.length() != 18) {
                return value;
            }
            Boolean identity = (Boolean) ctxt.getAttribute("identity");
            if (null == identity || !identity) {
                return value;
            }
            if (value.contains("*")) {
                return null;
            }
            return value;
        }
    }
}